#!/bin/bash
#auto install docker and Create VM
#by wugk 2016-01-05
#Define PATH Varablies
IPADDR=`ifconfig |grep "Bcast"|awk '{print $2}'|cut -d: -f2|grep "192.168"|head -1`
GATEWAY=`route -n|grep "UG"|awk '{print $2}'|grep "192.168"|head -1`
DOCKER_IPADDR=$1
IPADDR_NET=`ifconfig |grep "Bcast"|awk '{print $2}'|cut -d: -f2|grep "192.168"|head -1|awk -F. '{print $1"."$2"."$3".""xxx"}'`

NETWORK=(
    HWADDR=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $1}'`
    IPADDR=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $2}'`
    NETMASK=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $3}'`
    GATEWAY=`route -n|grep "UG"|awk '{print $2}'`
)

if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" ];then

	echo -e "\033[32m---------------------------------\033[0m"
	echo -e "\033[32mPlease exec $0 IPADDR CPU(C) MEM(G) DISK(G),example $0 $IPADDR_NET 16 32 50\033[0m" 
	exit 0
fi


CPU=`expr $2 - 1`
if [ ! -e /usr/bin/bc ];then
	yum install bc -y >>/dev/null 2>&1
fi
MEM_F=`echo $3 \* 1024|bc`
MEM=`printf "%.0f\n" $MEM_F`
DISK=$4
USER=$5
REMARK=$6

ping $DOCKER_IPADDR -c 1 >>/dev/null 2>&1

if [ $? -eq 0 ];then

	echo -e "\033[32m---------------------------------\033[0m"
	echo -e "\033[32mThe IP address to be used,Please change other IP,exit.\033[0m"
	exit 0
fi

if [ ! -e /etc/init.d/docker ];then
	rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
	yum install docker-io -y
	yum install device-mapper*  -y
	/etc/init.d/docker start
	if [ $? -ne 0 ];then
		echo "Docker install error ,please check."
		exit 
	fi
fi	


cd  /etc/sysconfig/network-scripts/
    mkdir -p /data/backup/`date +%Y%m%d-%H%M`
    yes|cp ifcfg-eth* /data/backup/`date +%Y%m%d-%H%M`/
if
    [ -e /etc/sysconfig/network-scripts/ifcfg-br0 ];then
	echo
else
    cat >ifcfg-eth0 <<EOF
    DEVICE=eth0
    BOOTPROTO=none
    ${NETWORK[0]}
    NM_CONTROLLED=no
    ONBOOT=yes
    TYPE=Ethernet
    BRIDGE="br0"
    ${NETWORK[1]}
    ${NETWORK[2]}
    ${NETWORK[3]}
    USERCTL=no
EOF
    cat >ifcfg-br0 <<EOF
    DEVICE="br0"
    BOOTPROTO=none
    ${NETWORK[0]}
    IPV6INIT=no
    NM_CONTROLLED=no
    ONBOOT=yes
    TYPE="Bridge"
    ${NETWORK[1]}
    ${NETWORK[2]}
    ${NETWORK[3]}
    USERCTL=no
EOF

    /etc/init.d/network restart

fi


echo 'Your can restart Ethernet Service: /etc/init.d/network restart !'
echo '---------------------------------------------------------'

cd -
#######create docker container
service docker status >>/dev/null
if [ $? -ne 0 ];then 
	/etc/init.d/docker restart
fi

NAME="Docker$$_`echo $DOCKER_IPADDR|awk -F"." '{print $(NF-1)"_"$NF}'`"
IMAGES=`docker images|grep -v "REPOSITORY"|grep -v "none"|head -1|awk '{print $1}'`
CID=$(docker run -itd --cpuset-cpus=0-$CPU -m ${MEM}m --net=none --name=$NAME $IMAGES /bin/bash)

if [ -z $IMAGES ];then
	echo "Plesae Download Docker Centos Images,you can to be use docker search centos,and docker pull centos6.5-ssh,exit 0"
	exit 0
fi

if [ ! -f /usr/local/bin/pipework ];then
		yum install wget unzip zip -y
		wget https://github.com/jpetazzo/pipework/archive/master.zip
        unzip master
        cp pipework-master/pipework  /usr/local/bin/
        chmod +x /usr/local/bin/pipework
        rm -rf master
fi

ip netns >>/dev/null
if [ $? -ne 0 ];then
		rpm -e iproute --nodeps
        rpm -ivh https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm
fi
pipework br0 $NAME  $DOCKER_IPADDR/24@$IPADDR

docker ps -a |grep "$NAME"

DEV=$(basename $(echo /dev/mapper/docker-*-$CID)) 
dmsetup table $DEV | sed "s/0 [0-9]* thin/0 $((${DISK}*1024*1024*1024/512)) thin/" | dmsetup load $DEV 
dmsetup resume $DEV 
resize2fs /dev/mapper/$DEV 
docker start $CID 
docker logs $CID

LIST="docker_vmlist.csv"
if [ ! -e $LIST ];then
	echo "编号,容器ID,容器名称,CPU,内存,硬盘,容器IP,宿主机IP,使用人,备注" >$LIST
fi
###################
NUM=`cat docker_vmlist.csv |grep -v CPU|tail -1|awk -F, '{print $1}'`
if [[ $NUM -eq "" ]];then
        NUM="1"
else
        NUM=`expr $NUM + 1`
fi
##################	
echo -e "\033[32mCreate virtual client Successfully.\n$NUM `echo $CID|cut -b 1-12` $NAME $2C ${MEM}M ${DISK}G $DOCKER_IPADDR $IPADDR $USER $REMARK\033[0m"
if [ -z $USER ];then
	USER="NULL"
	REMARK="NULL"
fi
echo $NUM,`echo $CID|cut -b 1-12`,$NAME,${2}C,${MEM}M,${DISK}G,$DOCKER_IPADDR,$IPADDR,$USER,$REMARK >>$LIST
rm -rf docker_vmlist_*
iconv -c -f utf-8 -t gb2312 docker_vmlist.csv -o docker_vmlist_`date +%H%M`.csv
